
#ifndef _TTG_PIPELINE_LOOP_
#define _TTG_PIPELINE_LOOP_
#include "Common/Definitions.h"

#include <vector>

#include "Runtime.Low/Threads.h"

namespace ttg_internal
{
	namespace pipeline
	{
		//Defenition of used class.
		class NavigatorNode;

		//Auxiliary class, which keeps information about loop nodes.
		class Loop
		{
			private:
				ttg::threads::AtomicInt active_nodes;					//count of active nodes (nodes, that process data).
				std::vector<NavigatorNode *> nodes;
				std::vector<ttg::threads::AtomicInt> node_states;	//states of node (processes task or not).
				size_t node_with_tasks;					//id of node, which can produce data
			private:
				//Tries to find path from "first" to "last".
				//If such path exists stores it to "path" and returns true, false otherwise.
				bool findPath(std::vector<NavigatorNode *> &path, NavigatorNode *first, NavigatorNode *last);
			public:
				//Creates loop, which connects "last" node with "first".
				//So "last" node can send data to first.
				Loop(NavigatorNode *first, NavigatorNode *last);
				//Returns true, if node should start finishing iteration.
				bool shouldFinishIteration(size_t node_id, int active_inputs);
				//Notifies loop, that node had received data.
				void notify(size_t node_id, bool has_tasks);
				//Notifies loop that node has started iteration.
				void startIteration(size_t node_id);
				//Returns ID of given node.
				//If node doesn't belong to loop, throws Error.
				size_t getID(NavigatorNode *node);
		};
	}
}

#endif
